import { _decorator, Node, instantiate, v3, randomRangeInt, tween, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import { DragTetris_GameData } from "../Data/DragTetris_GameData";
import { DragTetris_BlockType, DragTetris_PosType } from "../Data/DragTetris_GameEnum";
import DragTetris_UIManager from "../Manager/DragTetris_UIManager";
import DragTetris_GamePanel from "../UI/DragTetris_GamePanel";
import DragTetris_GameConfig from "../Data/DragTetris_GameConfig";
import DragTetris_AssetsLoad from "../Manager/DragTetris_AssetsLoad";
import DragTetris_Grid from "../Game/DragTetris_Grid";
import DragTetris_BlockGroup from "../Game/DragTetris_BlockGroup";
import DragTetris_Block from "../Game/DragTetris_Block";

@ccclass('DragTetrisCardFunctions')
export default class DragTetris_CardFunctions {
    //    /** 创建格子 */
    static CreateGrid(_id: number, x: number, y: number): DragTetris_Grid {
        const node: Node = instantiate(DragTetris_AssetsLoad.Instance.Grid);
        const scrGrid: DragTetris_Grid = node.getComponent(DragTetris_Grid);
        scrGrid.init(_id, x, y);
        DragTetris_UIManager.Instance.Root_grid.addChild(node);
        return scrGrid;
    }
    //    /** 创建牌组 */
    static CreateBlock(_posType: DragTetris_PosType, _x: number, _y: number, _point: number, _color: number): DragTetris_Block {
        const node: Node = instantiate(DragTetris_AssetsLoad.Instance.Block);
        const block: DragTetris_Block = node.getComponent(DragTetris_Block);
        // DragTetris_GamePanel.Instance.GeneratePoint_blook_list[_x].addChild(node);
        block.init(_posType, _x, _y, _point, _color);

        // (_blockType: BlockType, _posType: PosType, _x: number, _y: number, _point: number, _color: number)
        return block;
    }
    //    /** 创建牌组 */
    static CreateBlockGroup(_id, _x: number, _y: number, _color: number, posType: DragTetris_PosType): DragTetris_BlockGroup {
        const node: Node = instantiate(DragTetris_AssetsLoad.Instance.BlockGroup);
        const blockGroup: DragTetris_BlockGroup = node.getComponent(DragTetris_BlockGroup);
        DragTetris_GamePanel.Instance.GeneratePoint_blook_list[_x].addChild(node);
        blockGroup.init(_id, _x, _y, _color, posType);
        DragTetris_GameData.arrCardGroups[_x] = blockGroup;
        // DragTetris_GameData.arrCardGroups.push(blockGroup);

        // DragTetris_GameData.SaveData();

        return blockGroup;
    }
    //    //    /** 创建牌组 */
    static CreateBlockGroup_temp(_id, _x: number, _y: number, _color: number, posType: DragTetris_PosType): DragTetris_BlockGroup {
        const node: Node = instantiate(DragTetris_AssetsLoad.Instance.BlockGroup);
        const blockGroup: DragTetris_BlockGroup = node.getComponent(DragTetris_BlockGroup);
        DragTetris_GamePanel.Instance.GeneratePoint_blook_list_temp[_x].addChild(node);
        blockGroup.init(_id, _x, _y, _color, posType);
        DragTetris_GameData.arrCardGroups_temp[_x] = blockGroup;

        // DragTetris_GameData.arrCardGroups.push(blockGroup);
        return blockGroup;
    }
    static async 随机创建一个BlockGroup(index) {
        DragTetris_GameData.arrCardGroups_temp[index] = null;
        DragTetris_GameData.arrCardGroups[index] = null;

        let y = -1;
        let color = DragTetris_CardFunctions.GetRandomColor();
        let id = randomRangeInt(0, DragTetris_GameConfig.方块种类数量);
        let posType = DragTetris_PosType.下面操作台中;



        DragTetris_CardFunctions.CreateBlockGroup_temp(id, index, y, color, posType);


        let newBlockGroup = DragTetris_CardFunctions.CreateBlockGroup(id, index, y, color, posType);
        // DragTetris_GameData.SaveData();


        {
            let d1 = 500;
            let d2 = 4;
            // blockGroup.node.y -= d1;
            let p = newBlockGroup.node.position;
            newBlockGroup.node.setPosition(p.x, p.y - d1, p.z);
            tween(newBlockGroup.node)
                .by(0.4, { position: v3(0, d1 + d2) })
                .by(0.1, { position: v3(0, -d2) })
                // .by(0.4, { y: +(d1 + d2) }, { easing: "" })
                // .by(0.1, { y: -d2 }, { easing: "" })
                .call(() => {
                })
                .start();
        }
    }
   
    static GetGrid(x: number, y: number) {
        if (DragTetris_GameData.arrGrids[x] && DragTetris_GameData.arrGrids[x][y]) {
            return DragTetris_GameData.arrGrids[x][y];
        }

        return null;
    }
    static Get_arrCardGroups() {
        return DragTetris_GameData.arrCardGroups;
    }
    static Get_arrCardGroup_grid() {
        const arr = DragTetris_GameData.arrCardGroups.filter(number => number.posType === DragTetris_PosType.方格中);
        return arr;
    }
    //    /** 获取随机颜色 */
    static GetRandomColor() {
        let color: number = randomRangeInt(0, 9);

        return color;
    }
    //    /** 获取随机点数 */
    static GetRandomPoint() {
        let point: number = randomRangeInt(1, 6 + 1);

        return point;
    }
    //    // 获取Item
    static GetItem(arr, x: number, y: number) {
        if (arr[x] && arr[x][y]) {
            return arr[x][y];
        }

        return null;
    }
    //    // 
    static SetAllGridCanPlaceByBlock(block_target: DragTetris_Block) {
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const grid_0: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                if (grid_0.block) {
                    grid_0.canPlace = false;
                }

                let up: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y + 1);
                let down: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y - 1);
                let left: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x - 1, y);
                let right: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x + 1, y);

                let array = [up, down, left, right];
                for (let index = 0; index < array.length; index++) {
                    const grid_4: DragTetris_Grid = array[index];
                    if (block_target && grid_4 && grid_4.block) {
                        if (block_target.color !== grid_4.block.color && block_target.point !== grid_4.block.point) {
                            grid_0.canPlace = false;
                        }
                    }
                }
            }
        }
    }
    static RefreshAllGridCanPlace() {
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const grid_0: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                if (grid_0) {
                    grid_0.RefreshPlace();
                }
            }
        }
    }
    static SetAllGridCanPlaceForce(active: boolean) {
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const grid_0: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                if (grid_0) {
                    grid_0.canPlace = active;
                    grid_0.RefreshPlace();
                }
            }
        }
    }
    //    // 
    static AllGridSelect(active: boolean) {
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                grid.Select(active);
            }
        }
    }
    static GetScore() {
        let score: number = 10;
        return score;
    }
    //    // 是否存在Grid
    static HasGrid(x: number, y: number): boolean {
        if (DragTetris_GameData.arrGrids[x] && DragTetris_GameData.arrGrids[x][y] && DragTetris_GameData.arrGrids[x][y].id >= 0) {
            return true;
        } else {
            return false;
        }
    }
    static 是否可以放得下() {
        let null_count = 0;

        // console.log(`DragTetris_GameData.arrCardGroups_temp`, DragTetris_GameData.arrCardGroups_temp);

        for (let index_arrCardGroups_temp = 0; index_arrCardGroups_temp < DragTetris_GameData.arrCardGroups_temp.length; index_arrCardGroups_temp++) {
            const group1 = DragTetris_GameData.arrCardGroups_temp[index_arrCardGroups_temp];

            if (group1) {
                group1.node.setScale(1, 1, 1);
                for (let index_1 = 0; index_1 < 4; index_1++) {
                    group1.node.angle += 90;

                    for (let x = 0; x < DragTetris_GameConfig.col; x++) {
                        for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                            const grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);

                            // let p1 = grid.node.convertToWorldSpaceAR(v3(0, 0))

                            let block1 = group1.block_list[0];

                            // let p1 = grid.node.convertToWorldSpaceAR(v3(0, 0));
                            // let p2 = block1.node.parent.convertToNodeSpaceAR(p1);
                            let p1 = grid.node.getComponent(UITransform).convertToWorldSpaceAR(new Vec3(0, 0, 0));
                            let p2 = block1.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p1);


                            let arrdiff = [];

                            for (let index = 0; index < group1.block_list.length; index++) {
                                const block = group1.block_list[index];
                                // let diff_pos = v3(block.node.x - block1.node.x, block.node.y - block1.node.y, 0);
                                let diff_pos = v3(block.node.position.x - block1.node.position.x, block.node.position.y - block1.node.position.y, 0);
                                arrdiff.push(diff_pos);
                            }

                            for (let index = 0; index < group1.block_list.length; index++) {
                                const block = group1.block_list[index];
                                let diff_pos = arrdiff[index];
                                let pos_new = v3(p2.x + diff_pos.x, p2.y + diff_pos.y);
                                block.node.position = pos_new;
                            }

                            let is是否契合 = DragTetris_CardFunctions.是否契合(group1);
                            if (is是否契合) {
                                return true;
                            }
                        }
                    }
                }
            }
            else {
                null_count++;
            }
        }

        console.log("null_count-----------------", null_count);
        console.log("DragTetris_GameData.arrCardGroups_temp.length-----------------", DragTetris_GameData.arrCardGroups_temp.length);

        if (null_count === DragTetris_GameData.arrCardGroups_temp.length) {
            console.log("是否可以放得下-----------------", true);

            return true;
        }

        console.log("是否可以放得下-----------------", false);

        return false;
    }
    static 是否契合(group1) {
        let count = 0;
        for (let index = 0; index < group1.block_list.length; index++) {
            const block: DragTetris_Block = group1.block_list[index];
            // let p1 = block.node.convertToWorldSpaceAR(v2(0, 0));
            // let p2 = NodeManager.Instance.Root_grid.convertToNodeSpaceAR(p1);
            // let targetPosWorld = GamePanel.Instance.ControlPanel.convertToWorldSpaceAR(Vec3.ZERO);

            //     let p1 = this.node.convertToWorldSpaceAR(v2(0, 0));

            // let p1 = block.node.convertToWorldSpaceAR(v3(0, 0));
            // let p2 = DragTetris_UIManager.Instance.Root_grid.convertToNodeSpaceAR(p1);
            let p1 = block.node.getComponent(UITransform)!.convertToWorldSpaceAR(new Vec3(0, 0, 0));
            let p2 = DragTetris_UIManager.Instance.Root_grid.getComponent(UITransform).convertToNodeSpaceAR(p1);

            const x_new = block.GetXByX(p2.x);
            const y_new = block.GetXByY(p2.y);

            let 当前格子是否是空地 = DragTetris_CardFunctions.当前格子是否是空地(x_new, y_new);
            if (当前格子是否是空地) {
                count++;
            }
        }

        if (count === group1.block_list.length) {
            return true;
        }

        return false;
    }
    static 当前格子是否是空地(x, y) {
        let grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y)
        // let stopBlock = DragTetris_CardFunctions.GetStopBlock(x, y)

        // if (grid && grid.block === null && !stopBlock) {
        if (grid && grid.block === null) {
            return true;
        }
        else {
            return false;
        }
    }
    static 获取所有空地() {
        let arr = [];
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const is_empty = DragTetris_CardFunctions.当前格子是否是空地(x, y);
                if (is_empty) {
                    let grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                    arr.push(grid);
                }
            }
        }

        return arr;
    }
    static 获取所有非空地() {
        let arr = [];
        for (let x = 0; x < DragTetris_GameConfig.col; x++) {
            for (let y = 0; y < DragTetris_GameConfig.row; y++) {
                const is_empty = DragTetris_CardFunctions.当前格子是否是空地(x, y);
                if (!is_empty) {
                    let grid: DragTetris_Grid = DragTetris_CardFunctions.GetGrid(x, y);
                    arr.push(grid);
                }
            }
        }

        return arr;
    }
}

// /** 调试 */
window["DragTetris_CardFunctions"] = DragTetris_CardFunctions;